//package com.cyj.springbootservice.interceptor;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.core.MethodParameter;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.http.server.ServletServerHttpRequest;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
//
//import java.time.Duration;
//
///**
// * @description: 响应结果写入到缓存
// * ResponseBodyAdvice是Spring提供的高级用法，会在结果被处理前进行拦截，拦截的逻辑自己实现
// * @Author C_Y_J
// * @create 2021-03-22 09:23
// **/
//@ControllerAdvice
//public class MyResponseBodyAdvice implements ResponseBodyAdvice {
//
//    @Autowired
//    @Qualifier(value = "redisCacheTemplate")
//    private RedisTemplate<String, String> redisTemplate;
//
//    private ObjectMapper mapper = new ObjectMapper();
//
//    @Override
//    public boolean supports(MethodParameter returnType, Class converterType) {
//        //支持的处理类型，这里仅对get和post进行处理
//        return returnType.hasMethodAnnotation(GetMapping.class) || returnType.hasMethodAnnotation(PostMapping.class);
//    }
//
//    @Override
//    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
//        try {
//            String redisKey = RedisCacheInterceptor.createRedisKey(((ServletServerHttpRequest) request).getServletRequest());
//            System.out.println("缓存的key："+redisKey);
//            String redisValue;
//            if (body instanceof String) {
//                redisValue = (String) body;
//            } else {
//                redisValue = mapper.writeValueAsString(body);
//            }
//            //存储到redis中
//            this.redisTemplate.opsForValue().set(redisKey, redisValue, Duration.ofHours(1));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return body;
//    }
//}
